package com.sanshow.charging.fragment;

import static com.blankj.utilcode.util.ThreadUtils.runOnUiThread;

import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ProcessUtils;
import com.blankj.utilcode.util.SPUtils;
import com.sanshow.charging.BackActivity;
import com.sanshow.charging.MainActivity;
import com.sanshow.charging.R;
import com.sanshow.charging.gson.DeviceInfo;
import com.sanshow.charging.gson.LoginInfo;
import com.sanshow.charging.util.ConstUtil;
import com.sanshow.charging.util.HttpUtil;
import com.sanshow.charging.util.StringUtil;
import com.sanshow.charging.util.Utility;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;


public class LoginDailogFragment extends DialogFragment implements View.OnClickListener {

    private EditText mUsername;
    private EditText mPassword;
    private Button btn;
    private ImageView iv;


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        //设置背景透明
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_login, null);
        iv = view.findViewById(R.id.login_iv);
        mUsername = view.findViewById(R.id.login_et1);
        btn = view.findViewById(R.id.login_btn);
        mPassword = view.findViewById(R.id.login_et2);
        iv.setOnClickListener(this);
        btn.setOnClickListener(this);

        builder.setView(view);
        return builder.create();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_btn:
                Toast toast = null;
                String userName = mUsername.getText().toString();
                String password = mPassword.getText().toString();
                if (userName.equals("")) {
                    toast = Toast.makeText(getActivity(), "用户名不能为空", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                    return;
                }
                if (password.equals("")) {
                    toast = Toast.makeText(getActivity(), "密码不能为空", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                    return;
                }

                //dismiss();
                //BackActivity.getInstance(getActivity());

                String md5Password = StringUtil.stringToMD5(password + ConstUtil.salt);
                String spUserName = SPUtils.getInstance().getString("u");
                String spPassword = SPUtils.getInstance().getString("p");
                if (userName.equals(spUserName) && md5Password.equals(spPassword)) {
                    dismiss();
                    BackActivity.getInstance(getActivity());
                } else {
                    toast = Toast.makeText(getActivity(), "用户名或密码错误", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.CENTER, 0, 0);
                    toast.show();
                    String content = "saveName:" + spUserName + ",savePass:" + spPassword + ",inputName:" +
                            userName + ",inputPass:" + md5Password;
                    uploadLog(content);
                    return;
                }

                //requestDeviceLogin(userName, password);
                break;

            case R.id.login_iv:
                dismiss();
        }
    }

    public void requestDeviceLogin(String userName, String password) {
        String md5Password = StringUtil.stringToMD5(password + ConstUtil.salt);
        HttpUtil.sendDeviceLoginHttpRequest(userName, md5Password, new Callback() {
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                final String responseText = response.body().string();
                final LoginInfo loginInfo = Utility.handleLoginInfoResponse(responseText);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (loginInfo != null && "1".equals(loginInfo.code)) {
                            dismiss();
                            BackActivity.getInstance(getActivity());
                        } else {
                            Toast toast = Toast.makeText(getActivity(), "用户名或密码错误", Toast.LENGTH_SHORT);
                            toast.setGravity(Gravity.CENTER, 0, 0);
                            toast.show();
                            return;
                        }
                    }
                });
            }

            @Override
            public void onFailure(Call call, IOException e) {
                e.printStackTrace();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getActivity(), "登录请求失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }

        });
    }

    public void uploadLog(String log){
        HttpUtil.sendUploadLog(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.d("tianmin","上传日志结果失败=" + e.getLocalizedMessage());
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                Log.d("tianmin","上传日志成功=" + response.body().string());
            }
        },log);
    }

}
